﻿@typeparam TItem
@inherits BaseComponent
<Row class="@ClassNames" style="@StyleNames" @attributes="@Attributes">
    <Column ColumnSize="ColumnSizeIsAuto" Flex="FlexRow">
        @if ( GetButtonRowPosition() == PagerElementPosition.Start || GetButtonRowPosition() == PagerElementPosition.Default )
        {
            @buttonRowFragment
        }
        @if ( GetColumnChooserPosition() == PagerElementPosition.Start || GetColumnChooserPosition() == PagerElementPosition.Default )
        {
            @columnChooserFragment
        }
        @if ( GetPaginationPosition() == PagerElementPosition.Start || ( GetPaginationPosition() == PagerElementPosition.Default && !ShowButtonRow ) )
        {
            @paginationFragment
        }
        @if ( GetTotalItemsPosition() == PagerElementPosition.Start )
        {
            @totalItemsFragment
        }
    </Column>

    <Column ColumnSize="ColumnSizeIsAuto" Flex="FlexRow" Margin="MarginIsAutoOnX">
        @if ( GetButtonRowPosition() == PagerElementPosition.Center )
        {
            @buttonRowFragment
        }
        @if ( GetColumnChooserPosition() == PagerElementPosition.Center )
        {
            @columnChooserFragment
        }
        @if ( GetPaginationPosition() == PagerElementPosition.Center || ( GetPaginationPosition() == PagerElementPosition.Default && ShowButtonRow ) )
        {
            @paginationFragment
        }
        @if ( GetTotalItemsPosition() == PagerElementPosition.Center )
        {
            @totalItemsFragment
        }
    </Column>

    <Column ColumnSize="ColumnSizeIsAuto" Flex="FlexRowAlignItemsCenterJustifyContentEnd" Margin="MarginIsAutoFromStart">
        @if ( GetButtonRowPosition() == PagerElementPosition.End )
        {
            @buttonRowFragment
        }
        @if ( GetPaginationPosition() == PagerElementPosition.End )
        {
            @paginationFragment
        }
        @if ( GetTotalItemsPosition() == PagerElementPosition.End || GetTotalItemsPosition() == PagerElementPosition.Default )
        {
            @totalItemsFragment
        }
        @if ( GetColumnChooserPosition() == PagerElementPosition.End )
        {
            @columnChooserFragment
        }
    </Column>
</Row>

@code {
    private RenderFragment buttonRowFragment => __builder =>
    {
        @if ( ShowButtonRow )
        {
            <Field Display="DisplayInlineBlock">
                @{
                    var newButtonString = Localizer.Localize( ParentDataGrid.Localizers?.NewButtonLocalizer, "New" );
                    var editButtonString = Localizer.Localize( ParentDataGrid.Localizers?.EditButtonLocalizer, "Edit" );
                    var deleteButtonString = Localizer.Localize( ParentDataGrid.Localizers?.DeleteButtonLocalizer, "Delete" );
                    var clearFilterButtonString = Localizer.Localize( ParentDataGrid.Localizers?.ClearFilterButtonLocalizer, "Clear Filter" );
                    var saveBatchButtonString = Localizer.Localize( ParentDataGrid.Localizers?.SaveBatchButtonLocalizer, "Save Changes" );
                    var cancelBatchButtonString = Localizer.Localize( ParentDataGrid.Localizers?.CancelBatchButtonLocalizer, "Cancel Changes" );

                    @ButtonRowTemplate( new()
                    {
                        NewCommand = new()
                        {
                            Clicked = NewClick,
                            LocalizationString = newButtonString,
                        },
                        EditCommand = new()
                        {
                            Clicked = EditClick,
                            LocalizationString = editButtonString,
                            Item = SelectedRow
                        },
                        DeleteCommand = new()
                        {
                            Clicked = DeleteClick,
                            LocalizationString = deleteButtonString,
                            Item = SelectedRow
                        },
                        ClearFilterCommand = new()
                        {
                            Clicked = ClearFilterClick,
                            LocalizationString = clearFilterButtonString,
                        },
                        SaveBatchCommand = new()
                        {
                            Clicked = SaveBatchClick,
                            LocalizationString = saveBatchButtonString,
                        },
                        CancelBatchCommand = new()
                        {
                            Clicked = CancelBatchClick,
                            LocalizationString = cancelBatchButtonString,
                        }
                    } );
                }
            </Field>
        }
    };

    private RenderFragment columnChooserFragment => __builder =>
    {
        @if ( ShowColumnChooser )
        {
            var columnChooserString = Localizer.Localize( ParentDataGrid.Localizers?.ColumnChooserLocalizer, "Columns" );

            <Field Display="DisplayInlineBlock" Margin="ColumnChooserMargin">
                <Dropdown @bind-Visible="@dropdownColumnChooserVisible">
                    <DropdownToggle @onclick:stopPropagation>
                        <Icon Name="@IconName.Table" Margin="MarginIs1FromStart" Clicked="() => { }" /> @columnChooserString
                    </DropdownToggle>
                    <DropdownMenu MaxMenuHeight="200px">
                        @if ( ParentDataGrid.ColumnChooserTemplate is null )
                        {
                            @foreach ( var column in ParentDataGrid.GetColumns().Where( x => x.IsRegularColumn ) )
                            {
                                <DropdownItem ShowCheckbox Checked=@column.Displaying CheckedChanged="@(value => ColumnDisplayingChanged( column, value ))">@column.Caption</DropdownItem>
                            }
                        }
                        else
                        {
                            @ParentDataGrid.ColumnChooserTemplate(new(
                                ParentDataGrid.GetColumns().Where( x => x.IsRegularColumn ),
                                EventCallback.Factory.Create<ColumnDisplayChangedEventArgs<TItem>>( this, ( x ) => ColumnDisplayingChanged( x.Column, x.Display ) ) ) );
                        }
                    </DropdownMenu>
                </Dropdown>
            </Field>
        }
    };

    private RenderFragment totalItemsFragment => __builder =>
    {
        @if ( ShowPager && !ParentDataGrid.Virtualize )
        {
            <Field Margin="MarginIsAutoOnYIs2FromStart" Display="DisplayNoneOnMobileInlineBlockOnDesktop">
                @if ( TotalItemsTemplate is not null )
                {
                    @TotalItemsTemplate( PaginationContext )
                }
                else
                {
                    var totalItems = PaginationContext?.TotalItems ?? default;

                    if ( totalItems == 0 )
                    {
                        <Text>@Localizer.Localize( ParentDataGrid.Localizers?.NumbersOfItemsLocalizer, "{0} - {1} of {2} items", 0, 0, 0 )</Text>
                    }
                    else
                    {
                        var curStart = ( ( PaginationContext.CurrentPage - 1 ) * PaginationContext.CurrentPageSize + 1 );
                        var curEnd = Math.Min( PaginationContext.CurrentPage * PaginationContext.CurrentPageSize, totalItems );
                        <Text>@Localizer.Localize( ParentDataGrid.Localizers?.NumbersOfItemsLocalizer, "{0} - {1} of {2} items", curStart, curEnd, totalItems )</Text>
                    }
                }
            </Field>
            <Field Margin="MarginIsAutoOnYIs2FromStart" Display="DisplayInlineBlockNoneOnDesktop">
                @if ( TotalItemsShortTemplate is not null )
                {
                    @TotalItemsShortTemplate( PaginationContext )
                }
                else
                {
                    var text = $"{PaginationContext.TotalItems} {Localizer.Localize( ParentDataGrid.Localizers?.ItemsLocalizer, "items" )}";
                    <Text>@text</Text>
                }
            </Field>
        }
    };

    private RenderFragment paginationFragment => __builder =>
    {
        @if ( ShowPager && !ParentDataGrid.Virtualize )
        {
            <Pagination Size="@GetSize()">
                <PaginationItem Disabled="@(PaginationContext.CurrentPage <= 1)">
                    <PaginationLink Page="first" Clicked="@OnPaginationItemClick">
                        @if ( FirstPageButtonTemplate is not null )
                        {
                            @FirstPageButtonTemplate
                        }
                        else
                        {
                            @Localizer.Localize( ParentDataGrid.Localizers?.FirstPageButtonLocalizer, "First" )
                        }
                    </PaginationLink>
                </PaginationItem>
                <PaginationItem Disabled="@(PaginationContext.CurrentPage <= 1)">
                    <PaginationLink Page="prev" Clicked="@OnPaginationItemClick">
                        @if ( PreviousPageButtonTemplate is not null )
                        {
                            @PreviousPageButtonTemplate
                        }
                        else
                        {
                            @Localizer.Localize( ParentDataGrid.Localizers?.PreviousPageButtonLocalizer, "Prev" )
                        }
                    </PaginationLink>
                </PaginationItem>
                @for ( int i = PaginationContext.FirstVisiblePage; i <= PaginationContext.LastVisiblePage; ++i )
                {
                    var pageNumber = i;
                    var pageNumberString = i.ToString();
                    var pageActive = pageNumber == PaginationContext.CurrentPage;

                    <PaginationItem Flex="FlexInlineFlexOnTabletJustifyContentCenterOnTablet" Display="DisplayNoneOnMobileInlineFlexRowOnTablet" Disabled="@(pageNumber == PaginationContext.CurrentPage)" Active="@pageActive">
                        <PaginationLink Page="@pageNumberString" Clicked="@OnPaginationItemClick">
                            @if ( PageButtonTemplate is not null )
                            {
                                @PageButtonTemplate( new( pageNumber, pageActive ) )
                            }
                            else
                            {
                                @pageNumberString
                            }
                        </PaginationLink>
                    </PaginationItem>
                }
                <PaginationItem Display="DisplayInlineFlexRowNoneOnTablet">
                    @if ( PageSelectorTemplate is not null )
                    {
                        @PageSelectorTemplate( PaginationContext )
                    }
                    else
                    {
                        <Select @bind-SelectedValue="@PaginationContext.CurrentPage" Size="GetSize()">
                            @for ( int i = PaginationContext.FirstVisiblePage; i <= PaginationContext.LastVisiblePage; ++i )
                            {
                                var pageNumber = i;
                                <SelectItem Value="@pageNumber">@pageNumber</SelectItem>
                            }
                        </Select>
                    }
                </PaginationItem>
                <PaginationItem Disabled="@(PaginationContext.CurrentPage >= PaginationContext.LastPage)">
                    <PaginationLink Page="next" Clicked="@OnPaginationItemClick">
                        @if ( NextPageButtonTemplate is not null )
                        {
                            @NextPageButtonTemplate
                        }
                        else
                        {
                            @Localizer.Localize( ParentDataGrid.Localizers?.NextPageButtonLocalizer, "Next" )
                        }
                    </PaginationLink>
                </PaginationItem>
                <PaginationItem Disabled="@(PaginationContext.CurrentPage >= PaginationContext.LastPage)">
                    <PaginationLink Page="last" Clicked="@OnPaginationItemClick">
                        @if ( LastPageButtonTemplate is not null )
                        {
                            @LastPageButtonTemplate
                        }
                        else
                        {
                            @Localizer.Localize( ParentDataGrid.Localizers?.LastPageButtonLocalizer, "Last" )
                        }
                    </PaginationLink>
                </PaginationItem>
                @if ( PaginationContext.ShowPageSizes )
                {
                    <PaginationItem Margin="MarginIs0FromBottomIs3FromStart">
                        @if ( PageSizesTemplate is not null )
                        {
                            @PageSizesTemplate( PaginationContext )
                        }
                        else
                        {
                            <Select Size="@GetSize()" @bind-SelectedValue="@PaginationContext.CurrentPageSize">
                                @foreach ( var curPageSize in PaginationContext.PageSizes )
                                {
                                    <SelectItem Value="@curPageSize">@curPageSize</SelectItem>
                                }
                            </Select>
                        }
                    </PaginationItem>
                    <Field Margin="MarginIsAutoOnYIs2FromStart" Display="DisplayNoneOnMobileInlineFlexRowOnDesktop">
                        @if ( ItemsPerPageTemplate is not null )
                        {
                            @ItemsPerPageTemplate
                        }
                        else
                        {
                            <Text>@Localizer.Localize( ParentDataGrid.Localizers?.ItemsPerPageLocalizer, "items per page" )</Text>
                        }
                    </Field>
                }
            </Pagination>
        }
    };
}